package com.study.controller;

import com.study.common.Result;
import com.study.common.ResultCode;
import com.study.entity.system.City;
import com.study.service.CityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author wfw
 * @Date 2020/07/14 09:01
 */
@CrossOrigin
@RestController
@RequestMapping("sys/city")
public class CityController {
    @Autowired
    private CityService cityService;

    @PostMapping("")
    public Result save(@RequestBody City city) {
        cityService.add(city);
        return new Result(ResultCode.SUCCESS);
    }

    @PutMapping("{id}")
    public Result update(@PathVariable(value = "id") String id, @RequestBody City city) {
        city.setId(id);
        cityService.update(city);
        return new Result(ResultCode.SUCCESS);
    }

    @DeleteMapping("{id}")
    public Result delete(@PathVariable(value = "id") String id) {
        cityService.deleteById(id);
        return new Result(ResultCode.SUCCESS);
    }

    @GetMapping("{id}")
    public Result findById(@PathVariable(value = "id") String id) {
        City city = cityService.findById(id);
        return new Result(ResultCode.SUCCESS, city);
    }

    @GetMapping("")
    public Result findAll() {
        List<City> list = cityService.findAll();
        return new Result(ResultCode.SUCCESS, list);
    }
}
